package com.usefullc.download.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.usefullc.system.param.FeedbackSubmitParam;
import com.usefullc.system.service.FeedbackService;
import com.usefullc.download.vo.UploadFileResultVo;
import com.usefullc.system.controller.BaseController;
import com.usefullc.system.controller.WebResultBuilder;
import com.usefullc.system.entity.WebResult;
import com.usefullc.system.service.file.FileResService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: Sampson
 * @date: 2022-01-10 15:28
 */
@RestController
@RequestMapping(path = "/download/feedback/")
@Api( tags="反馈管理")
public class FeedbackController extends BaseController {

    @Value("${usefullc.api.host}")
    private String apiHost;

    @Autowired
    private FeedbackService feedbackService;

    @Qualifier("localFileUploadService")
    @Autowired
    private FileResService fileResService;

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "问题建议反馈", response = WebResult.class)
    @PostMapping(path = "submit", produces = "application/json")
    public WebResult submit(@RequestBody FeedbackSubmitParam param) {
        feedbackService.submit(param);
        return WebResultBuilder.successResult();
    }

    @ApiOperation(value = "文件上传", response = WebResult.class)
    @PostMapping(path = "upload", produces = "application/json")
    public UploadFileResultVo upload(MultipartFile file, String biz) {
        UploadFileResultVo uploadFileResultVo = new UploadFileResultVo();
        try {
            byte[] bytes = file.getBytes();
            String oppositeFilePath = fileResService.upload(bytes,file.getOriginalFilename(),biz);

            uploadFileResultVo.setErrno(0);
            Map<String,String> data = new HashMap();
            data.put("url",apiHost+"/system/common/res"+oppositeFilePath);
            uploadFileResultVo.setData(data);

        } catch (IOException e) {
            logger.error("upload error",e);
            uploadFileResultVo.setErrno(1);
            uploadFileResultVo.setMessage("上传图片失败");
//            throw new BizException(e);
        }
        return uploadFileResultVo;
    }


}
